package com.justgame.project.service.system.controller;

import com.justgame.project.cloud.base.entity.CurrentUser;
import com.justgame.project.cloud.base.entity.FileSizeUnit;
import com.justgame.project.cloud.base.entity.OssFile;
import com.justgame.project.cloud.common.constant.Result;
import com.justgame.project.cloud.common.enums.FileTypeEnum;
import com.justgame.project.cloud.common.util.FileUtils;
import com.justgame.project.cloud.common.util.OssManager;
import com.justgame.project.service.system.dto.SysUserSocialInfoDTO;
import com.justgame.project.service.system.entity.SysUserInfo;
import com.justgame.project.service.system.entity.SysUserOperationLog;
import com.justgame.project.service.system.entity.SysUserSocialInfo;
import com.justgame.project.service.system.service.ISysUserOperationLogService;
import com.justgame.project.service.system.service.ISysUserSocialInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.Duration;

/**
 *
 * 用户社交信息表控制器
 *
 * @@tableName sys_user_social_info
 * @author Zwb
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/sys/user/social/info")
@Api("用户社交信息表控制器")
public class SysUserSocialInfoController {
    private final OssManager ossManager;
    private final ISysUserSocialInfoService sysUserSocialInfoService;
    private final ISysUserOperationLogService sysUserOperationLogService;

    @PostMapping("/update-nickname")
    @RequiresAuthentication
    @ApiOperation("修改昵称")
    public Result<?> updateNickname(CurrentUser currentUser,@RequestBody SysUserSocialInfoDTO dto){
        Assert.hasLength(dto.getNickname(),"昵称不能为空");
        Assert.isTrue(dto.getNickname().length() > 3 && dto.getNickname().length() < 30,"昵称长度限制3~30位");
        sysUserOperationLogService.doOperation(currentUser, SysUserOperationLog.Operation.UPDATE_NICKNAME, Duration.ofDays(1));
        Assert.isTrue(sysUserSocialInfoService.countByNicknameIs(dto.getNickname()) == 0,"该昵称已被其他用户使用，换一个吧？");
        SysUserSocialInfo socialInfo = sysUserSocialInfoService.findByUserInfoIdIs(currentUser.getId()).orElseThrow();
        return Result.status(sysUserSocialInfoService.updateByNickname(socialInfo,dto.getNickname()));
    }

    @PostMapping("/upload-avatar")
    @RequiresAuthentication
    @ApiOperation("上传头像")
    public Result<?> uploadAvatar(CurrentUser currentUser,
                                  @RequestParam("avatar")MultipartFile avatar){
        return Result.ok(sysUserSocialInfoService.updateAvatar(currentUser,avatar));
    }


}